import { pay } from "@/api/pay.js"
import {
	router,
	toLogin
} from '@/router'
import { storage } from "./storage";

var orderId = ''
const wxminiproPay = function (params){
	let that = this;
	return new Promise((resolve, reject)=>{
		//创建平台订单
		pay.createOrder(params).then(res => {
			if (res.data.code == 10000) {
				orderId = res.data.data.orderId
				if(params.payway=='lzpay'){
					//创建小程序支付
					pay.createLzPayforMinipro({
						orderId: orderId,
						openid: storage.getStorageDataSync('OPENID')
					}).then(res => {
						if(res.data.code == 10000){
							var orderInfo = res.data.data
							//拉起小程序支付
							uni.requestPayment({
								provider: "wxpay",
								timeStamp: orderInfo.timeStamp,
								nonceStr: orderInfo.nonceStr,
								package: orderInfo.package,
								signType: orderInfo.signType,
								paySign: orderInfo.paySign,
								success: function (res) {
									console.log('sendSuccess:', res);
									var data = {
										data: res,
										code: 10000,
										message: '支付成功'
									}
									resolve({data: data});
								},
								fail: function (err) {
									console.log('sendFail:',  err);
								}
							});
						}
					})
				}else if(params.payway=='wxpay'){
					// #ifdef MP-WEIXIN
					//创建小程序支付
					pay.createWxPayforMinipro({
						orderId: orderId,
						openid: storage.getStorageDataSync('OPENID')
					}).then(res => {
						if(res.data.code == 10000){
							var orderInfo = res.data.data
							//拉起小程序支付
							uni.requestPayment({
								provider: "wxpay",
								timeStamp: orderInfo.timeStamp,
								nonceStr: orderInfo.nonceStr,
								package: orderInfo.package,
								signType: orderInfo.signType,
								paySign: orderInfo.paySign,
								success: function (res) {
									console.log('sendSuccess:', res);
									var data = {
										data: res,
										code: 10000,
										message: '支付成功'
									}
									resolve({data: data});
								},
								fail: function (err) {
									console.log('sendFail:',  err);
								}
							});
						}
					})
					// #endif
					// #ifdef APP-PLUS
					pay.createWxPayforApp({
						orderId: orderId,
						openid: storage.getStorageDataSync('OPENID')
					}).then(res => {
						if(res.data.code == 10000){
							var orderInfo = res.data.data
							uni.requestPayment({
								provider:  'wxpay',
								orderInfo: orderInfo, //订单数据
								success: function (res) {
									// console.log('sendwxpaySuccess:', res);
									var data = {
										data: res,
										code: 10000,
										message: '支付成功'
									}
									resolve({data: data});
								},
								fail: function (err) {
									setTimeout(function () {
										uni.hideLoading();
									}, 30);
									console.log('sendwxpayFail:',  err);
								}
							});
						}
					})
					// #endif
					
				}else if(params.payway=='balance'){
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					pay.sendIncomsPay({
						orderId: orderId,
						scene: params.scene,
						payway: params.payway
					}).then(res => {
						if(res.data.code == 10000){
							resolve(res);
						}else{
							// uni.showModal({
							// 	content: '支付失败',
							// 	confirmText:'确定',
							// 	showCancel: false
							// })
						}
					})
				}else if(params.payway=='incomes'){
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					pay.sendIncomsPay({
						orderId: orderId,
						scene: params.scene,
						payway: params.payway
					}).then(res => {
						if(res.data.code == 10000){
							resolve(res);
						}else{
							// uni.showModal({
							// 	content: '支付失败',
							// 	confirmText:'确定',
							// 	showCancel: false
							// })
						}
					})
				}else{
					uni.showModal({
						content: 'payway参数未知',
						confirmText:'确定',
						showCancel: false
					})
				}
				
			}else if(res.data.code == 10001){
				console.log(res.data.message)
				router.push({
					name: 'payCode',
					params: res.data.data
				})
			}
		})
	})
}
const wxminiproOrderIdPay = function (params){
	let that = this;
	return new Promise((resolve, reject)=>{
		//创建小程序支付
		pay.createWxPayforMinipro({
			orderId: params.orderId,
			openid: storage.getStorageDataSync('OPENID')
		}).then(res => {
			if(res.data.code == 10000){
				var orderInfo = res.data.data
				//拉起小程序支付
				uni.requestPayment({
					provider: "wxpay",
					timeStamp: orderInfo.timeStamp,
					nonceStr: orderInfo.nonceStr,
					package: orderInfo.package,
					signType: orderInfo.signType,
					paySign: orderInfo.paySign,
					success: function (res) {
						console.log('sendSuccess:', res);
						resolve(res);
					},
					fail: function (err) {
						console.log('sendFail:',  err);
						
					}
				});
			}
		})
	})
}
export {
	wxminiproPay,
	wxminiproOrderIdPay
} 